<?php
namespace Common\Lib\Chejianding;
define("CURRENT_ROOT",dirname(__FILE__));
/**
 * 车鉴定Service
 */
class CJD{

	function __construct(){
		$this->urls = array(
			'check_user'=>'/rest/publicif/accountInfo',        //检查账号      done
			'buy_report'=>'/publicif/2.0/buy',                 //购买报告      done
			'get_order_info'=>'/rest/publicif/orderInfo',      // 获取报告信息
			'check_brand'=>'/rest/publicif/checkBrand',        //检查是否支持查询类型   done
			'brand_list' =>'/rest/publicif/brandList',		   //查询品牌列表	   done
			'report_data'=>'/rest/publicif/reportData',        //获取报告数据  方式一
			'report_data_2'=>'/rest/publicif/2.0/reportData',  //获取报告数据  方式二
			'report_data_3'=>'/publicif/2.1/reportData'        //获取报告数据  方式三
		);
		$this->uid='0064bd9d5a144f88bae23182d9602481';
		$this->pwd = '7915e54c2ba9cdc539422bc59657bf0b';
		$this->url='https://pif.chejianding.com';
	}
	/*请求*/
	function accountInfo($key,$option=array()){
		$time_s = time();
		$time = date('Y-m-d H:i:s',$time_s);
		$time_url = rawurlencode(date('Y-m-d H:i:s',$time_s));
		$url = $this->url.$this->urls[$key].'?uid='.rawurlencode($this->uid).'&time='.$time_url;
		switch ($key) {
			case 'check_user':
				$sign =  $this->getSign($this->pwd,$this->uid.$time);
				$url .= '&sign='.rawurlencode($sign);
				break;
			case 'buy_report':
				$sign =  $this->getSign($this->pwd,$this->uid.$option['vin'].$time);
				$url .= '&sign='.rawurlencode($sign).'&vin='.$option['vin'];
				break;
			case 'get_order_info':
				$sign =  $this->getSign($this->pwd,$this->uid.$option['order_id'].$time);
				$url .= '&sign='.rawurlencode($sign).'&orderId='.$option['order_id'];
				break;
			case 'check_brand':
				$sign =  $this->getSign($this->pwd,$this->uid.$option['vin'].$time);
				$url .= '&sign='.rawurlencode($sign);
				break;
			case 'brand_list':
				$sign =  $this->getSign($this->pwd,$this->uid.$time);
				$url .= '&sign='.rawurlencode($sign);
				break;
			case 'report_data':
				$sign =  $this->getSign($this->pwd,$this->uid.$option['order_id'].$time);
				$url .= '&sign='.rawurlencode($sign).'&orderId='.$option['order_id'];
				break;
			case 'report_data_2':
				$sign =  $this->getSign($this->pwd,$this->uid.$option['order_id'].$time);
				$url .= '&sign='.rawurlencode($sign).'&oid='.$option['order_id'];
				break;
			case 'report_data_3':
				$sign =  $this->getSign($this->pwd,$this->uid.$option['order_id'].$time);
				$url .= '&sign='.rawurlencode($sign).'&oid='.$option['order_id'];
				break;
			default:
				# code...
				break;
		}

		$data = array(
			'uid'=>rawurlencode($this->uid),
			'time'=>$time_url,
			'sign'=>rawurlencode($sign)
		);
		if(!empty($option)){
			$data = array_merge($data,$option);
		}
		$info = $this->https_post($url,$data);
		return  json_decode($info,true);
	}

	/*获取pem秘钥*/
	function sign($content) {
		$rsaPrivateKeyPem = CURRENT_ROOT.'/rsa_private_key.pem';
		$priKey = file_get_contents($rsaPrivateKeyPem);
		$res = openssl_get_privatekey($priKey);
		openssl_sign($content, $sign, $res);
		openssl_free_key($res);
		$sign = base64_encode($sign);
		return $sign;
	}


	/*获取加密签名*/
	function getSign($pwdMd5,$data){
		$content = $data . ($pwdMd5);

		return $this->sign($content);
	}

	/*curl请求*/
	function https_post($url,$data){
		$header[] = 'Content-Type: application/x-www-form-urlencoded';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER  , 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
		$re = curl_exec($ch);
		curl_close($ch);
		return $re;
	}
}

//$test = new CJD();
//$vin = array(
//	"0"=>"WVWVV7304A8003128", //大众辉腾
//	"1"=>"WAURGB4HXDN018347",//奥迪A8豪华版
//	"2"=>"LFMBEC4D7D0194201",//丰田锐志
//	"3"=>"LBVPS7100ASD71620",//宝马
//	"4"=>"1FT8X3BT9EEA25643",//福特猛禽
//	"5"=>"LBV5S7103ESF94361",//宝马535i
//);
//
////step 0  获取查询品牌列表
//$brand_list  = $test->accountInfo('brand_list');
//// response -> {"data":[{"needCon":0,"isEnable":0,"name":"保时捷","code":"Porsche"},{"needCon":0,"isEnable":1,"name":"长安马自达","code":"MazdaCA"},{"needCon":0,"isEnable":1,"name":"东风日产","code":"Nissan"},{"needCon":12,"isEnable":1,"name":"上海大众/斯柯达","code":"SHVW"},{"needCon":0,"isEnable":1,"name":"长安铃木","code":"SuzukiCA"},{"needCon":0,"isEnable":1,"name":"东风本田","code":"Honda"},{"needCon":0,"isEnable":1,"name":"荣威名爵","code":"RoeweMG"},{"needCon":0,"isEnable":1,"name":"英菲尼迪","code":"Infiniti"},{"needCon":0,"isEnable":1,"name":"东风雪铁龙","code":"Citroen"},{"needCon":0,"isEnable":1,"name":"奔驰","code":"Benz"},{"needCon":0,"isEnable":1,"name":"雪佛兰","code":"Chevrolet"},{"needCon":0,"isEnable":1,"name":"捷豹路虎","code":"LandRover"},{"needCon":0,"isEnable":1,"name":"江淮","code":"JAC"},{"needCon":0,"isEnable":1,"name":"东风标致","code":"Peugeot"},{"needCon":0,"isEnable":0,"name":"宾利","code":"Bentley"},{"needCon":0,"isEnable":1,"name":"菲亚特","code":"Fiat"},{"needCon":0,"isEnable":1,"name":"丰田（进口）","code":"Toyota"},{"needCon":0,"isEnable":1,"name":"雷克萨斯","code":"Lexus"},{"needCon":0,"isEnable":1,"name":"东风悦达起亚","code":"KIA"},{"needCon":0,"isEnable":1,"name":"玛莎拉蒂","code":"Maserati"},{"needCon":0,"isEnable":1,"name":"宝马","code":"BMW"},{"needCon":0,"isEnable":1,"name":"进口大众","code":"VW"},{"needCon":0,"isEnable":1,"name":"道奇/Jeep/克莱斯勒","code":"Chrysler"},{"needCon":0,"isEnable":1,"name":"现代","code":"Hyundai"},{"needCon":0,"isEnable":1,"name":"一汽丰田","code":"ToyotaYQ"},{"needCon":0,"isEnable":1,"name":"沃尔沃","code":"Volvo"},{"needCon":2,"isEnable":1,"name":"一汽大众/奥迪","code":"YQVW"},{"needCon":0,"isEnable":1,"name":"通用","code":"GM"},{"needCon":0,"isEnable":1,"name":"广汽本田","code":"HondaGQ"},{"needCon":0,"isEnable":1,"name":"长城/哈弗","code":"GWM"},{"needCon":1,"isEnable":0,"name":"一汽马自达","code":"MazdaYQ"}],"info":{"status":"1","message":"请求成功"}}
//
//
////step 1  检查账号
////$user_info  = $test->accountInfo('check_user','');
////step 2  购买报告
//// $get_order = $test->accountInfo('buy_report',array('vin'=>$vin[3]));
//// $get_order = json_decode($get_order,true);
//
//// if($get_order['info']['status']==1){
//// 	$order_id = $get_order['info']['orderId'];
//// }
///***  ['奥迪':'28e6dee6435c4d9cb00ac9838e92290e']
//['宝马':'37cda74343a14d2380690db0584ba9eb']
// */
//
//$order_id = '37cda74343a14d2380690db0584ba9eb';
////step 3 获取报告信息
//if($order_id){
//	$order_info  =  $test->accountInfo('get_order_info',array('order_id'=>$order_id));
////	response -> {"info":{"orderStatus":"3","status":"1","message":"订单处理中"}}
//	//var_dump($order_info);
//
//
//	//step 4 获取报告数据 1
//	// $car_info   = $test->accountInfo('report_data',array('order_id'=>$order_id));
//	// var_dump($car_info);die;
//
//	//step 4 获取报告数据 2
//	$car_info_2   = $test->accountInfo('report_data_2',array('order_id'=>$order_id));
//	var_dump($car_info_2);die;
//
//	//step 4 获取报告数据 3
//	$car_info_3   = $test->accountInfo('report_data_3',array('order_id'=>$order_id));
//	var_dump($car_info_3);die;
//}

